<?php

namespace app\api\controller\baiying;


use app\common\exception\UploadException;
use think\Config;
use think\Hook;
use baiying\lib\BYTokenClient;
use think\Db;

/**
 * 公共接口
 */
class Black extends Bybase {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize() {
        parent::_initialize();
    }



    public function addBlackPhone(){
        $request_data = $this->request->param();
        if(!empty($request_data['phone'])){
            $exist_info = db('black_mobile')->where('mobile',$request_data['phone'])->find();
            if(empty($exist_info)){
                $save_data = [
                    'mobile'=>$request_data['phone'],
                    'remark'=>empty($request_data['reason']) ? '' : $request_data['reason'],
                    'createtime'=>time(),
                    'uuid'=>empty($request_data['uuid']) ? '' : $request_data['uuid'],
                ];
                $insert_id = db('black_mobile')->insertGetId($save_data);
            }else if(!empty($exist_info['isexport'])){
                $this->error('此号码已被标记，请勿重复标记');
                exit;
            }else{
                $save_data = $exist_info;
                $insert_id = $exist_info['id'];
            }

            $response = $this->setBaiyingBlackphone($save_data);
            qqq('blackmobile',$response);
            if($response['code'] == 200){
                db('black_mobile')->where('id',$insert_id)->update([
                    'isexport' =>1,'exporttime'=>time()
                ]);
                $this->success('标记成功');
            }else{
                $this->error('标记失败');
            }


        }
    }



    public function setBaiyingBlackphone($request_data) {

        $client = new BYTokenClient($this->access_token);

        $method = 'byai.openapi.blackinfo.add'; //要调用的api名称
        $api_version = '1.0.0'; //要调用的api版本号


        $my_params = ['companyId' => $this->companyId,'mobile'=>$request_data['mobile'],'remark'=>$request_data['remark']];
        $my_files = [];

        $result = $client->post($method, $api_version, $my_params, $my_files);

        return $result;

    }




}
